package cs2340.droidceptiontodo.test;

import java.util.ArrayList;

import android.test.ActivityInstrumentationTestCase2;
import android.util.Log;
import cs2340.droidceptiontodo.*;
import java.util.ArrayList;
import java.util.List;
import cs2340.droidceptiontodo.Database;
import cs2340.droidceptiontodo.ToDoItem;
import android.test.AndroidTestCase;
import android.test.RenamingDelegatingContext;
import android.util.Log;
/**
 * 
 * @author Susmita Gorai
 * This class tests addItem() method of Database class by inserting a toDoItem and verifying it
 * by using JUnit library
 *
 */
public class AddToDoItemValidation extends AndroidTestCase {
	private Database db;
	private List<ToDoItem> items;
	private static final String TEST_FILE_PREFIX = "test_";
	
	/*
	 * (non-Javadoc)
	 * @see android.test.AndroidTestCase#setUp()
	 * This is initialization method to create a database instance and open it for reading/writing
	 */
	@Override
	protected void setUp() throws Exception {
        super.setUp();
        
        RenamingDelegatingContext context = new RenamingDelegatingContext(getContext(), TEST_FILE_PREFIX);
        
        items = new ArrayList<ToDoItem>();
        db = new Database(context);
        db.open();
        
	}
	
	/*
	 * Checks database is not null
	 */
	public void testPreConditions() {
	    assertNotNull(db);
	}
 
	/**
	 * This is the test method which inserts a todo item row in database and verifies it
	 */
	
	public final void testAddItem() {
		//Put item name input text of itemName text field
		//String itemNameStr = "Do extracredit";
		String itemNameStr = "mathwork4";
		//Put date input text of date picker
		int month = 6;
		int day = 20;
		int year = 2012;
		String dateStr = String.format("%d/%d/%d", month, day, year);
		//Put time input text of time picker
		int hour = 14;
		int min = 30;
		String amPM = "am";
		// Convert from military time
		if (hour >= 12) {
			if (hour > 12) {
				hour = hour-12;
			}
			amPM = "pm";
		}
		if (hour == 0) {
			hour = 12;
			amPM = "am";
		}
		String timeStr = String.format("%d:%02d" + amPM, hour, min);
		//Put description  input text of edittext10
		String descriptionStr = "Special credit";
		//Put category input text of edittext11
		String categoryStr = "School";
		//Put location input text of edittext12
		String locationStr = "GaTech";
		ToDoItem anItem = new ToDoItem(itemNameStr, "bat", descriptionStr,categoryStr, dateStr, timeStr,locationStr, false, null);
		//ToDoItem anItem = new ToDoItem("0", "bat", "details", "Work", "2015/04/05", "12:05", "location", true, null);
		Log.d("Debug", "item created...");
		try{
			db.addItem(anItem);
		}catch(Exception e){
			Log.d("Debug", e.toString());
		}
		ArrayList<ToDoItem> allItems = db.getItems("bat");
		boolean found = false;
		ToDoItem tempItem = null;
		for (int i=0; i<allItems.size(); i++)
		{
			tempItem = allItems.get(i);
			if (((tempItem.getName()).equals(itemNameStr))
				&& (tempItem.getDetails().equals(descriptionStr))
				&& (tempItem.getCategory().getName().equals(categoryStr))
				&& (tempItem.getDueDate().equals(dateStr))
				&& (tempItem.getDueTime().equals(timeStr))
				&& (tempItem.getLocation().equals(locationStr)))
			{
				found = true;
				break;
			}
		}
		Log.d("Debug", "Item addition verified");
		assertTrue("Add item found true", found == true);
	}
	
	

	/**
	 * This is the test method which update a todo item row in database and verifies it
	 */
	public final void testUpdateItem() {
		//Put item name input text of itemName text field
		//String itemNameStr = "Do extracredit";
		String itemNameStr = "mathwork5";
		//Put date input text of date picker
		int month = 6;
		int day = 20;
		int year = 2012;
		String dateStr = String.format("%d/%d/%d", month, day, year);
		//Put time input text of time picker
		int hour = 14;
		int min = 30;
		String amPM = "am";
		// Convert from military time
		if (hour >= 12) {
			if (hour > 12) {
				hour = hour-12;
			}
			amPM = "pm";
		}
		if (hour == 0) {
			hour = 12;
			amPM = "am";
		}
		String timeStr = String.format("%d:%02d" + amPM, hour, min);
		//Put description  input text from edittext10
		String descriptionStr = "Special credit";
		//Put category input text of edittext11
		String categoryStr = "School";
		//Put location input text of edittext12
		String locationStr = "GaTech";
		String rowId = "";
		ToDoItem anItem = new ToDoItem(itemNameStr, "bat", descriptionStr,categoryStr, dateStr, timeStr,locationStr, false, null);
		Log.d("Debug", "to do item created for insert");
		try{
			db.addItem(anItem);
		}catch(Exception e){
			Log.d("Debug", e.toString());
		}
		ArrayList<ToDoItem> allItems = db.getItems("bat");
		boolean found = false;
		ToDoItem tempItem = null;
		for (int i=0; i<allItems.size(); i++)
		{
			tempItem = allItems.get(i);
			if (((tempItem.getName()).equals(itemNameStr))
				&& (tempItem.getDetails().equals(descriptionStr))
				&& (tempItem.getCategory().getName().equals(categoryStr))
				&& (tempItem.getDueDate().equals(dateStr))
				&& (tempItem.getDueTime().equals(timeStr))
				&& (tempItem.getLocation().equals(locationStr)))
			{
				found = true;
				rowId = tempItem.getID();//rowId will be used to update
				Log.d("Debug", "Add Item successful- rowId " + rowId);
				break;
			}
		}
		//insertion was successful, try update now
		if(found)
		{
			Log.d("Debug", "Add Item insert successful, try update now ");
			//Modify some variable values 
			//change item name
			itemNameStr = "mathwork6";
			//Put date input text of date picker
			 month = 7;
			 day = 21;
			 year = 2013;
			 //change dateStr
			 dateStr = String.format("%d/%d/%d", month, day, year);
			//Put time input text of time picker
			 hour = 15;
			min = 31;
			amPM = "am";
			// Convert from military time
			if (hour >= 12) {
				if (hour > 12) {
					hour = hour-12;
				}
				amPM = "pm";
			}
			if (hour == 0) {
				hour = 12;
				amPM = "am";
			}
			//Change timeStr
			timeStr = String.format("%d:%02d" + amPM, hour, min);
			//change descriptionStr
			descriptionStr ="Not enough";
			//change loacationStr
			locationStr ="Peachtree St";
			//Todo item to be updated
			ToDoItem updateItem = new ToDoItem(itemNameStr, "bat", descriptionStr,categoryStr, dateStr, timeStr,locationStr, false, rowId);
			try{
				//update is done based on the rowid in database
				db.updateItem(updateItem);
			}catch(Exception e){
				Log.d("Debug", e.toString());
			}
			allItems = db.getItems("bat");
			found = false;
			tempItem = null;
			for (int i=0; i<allItems.size(); i++)
			{
				tempItem = allItems.get(i);
				//Log.d("Debug", "ID in DB:"+ tempItem.getID());
				//Log.d("Debug", "Item name in DB:"+ tempItem.getName());
				//Check all updated values are available
				if( (tempItem.getID().equals(rowId))
				    && ((tempItem.getName()).equals(itemNameStr))
					&& (tempItem.getDetails().equals(descriptionStr))
					&& (tempItem.getCategory().getName().equals(categoryStr))
					&& (tempItem.getDueDate().equals(dateStr))
					&& (tempItem.getDueTime().equals(timeStr))
					&& (tempItem.getLocation().equals(locationStr)))
				{
					found = true;
					Log.d("Debug", "Item update successful.");
					break;
				}
			}
			Log.d("Debug", "Item update verifying ");
			assertTrue("Update item found true", found == true);
		}
	}
	
	/**
	 * Closes the database
	 */
	
	@Override
	protected void tearDown() throws Exception {
	    super.tearDown();

	    db.close();
	    db = null;
	}

}

